/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.android1.amarena2d.android;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.PowerManager;
import com.android1.amarena2d.engine.Config;
import com.android1.amarena2d.engine.AmarenaEngine;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.surfaceview.FillResolutionStrategy;
import com.badlogic.gdx.backends.android.surfaceview.ResolutionStrategy;

/**
 * @author Christoph Widulle
 */
public abstract class AmarenaAndroidEngine extends AndroidApplication {

    private AmarenaEngine amarenaEngine;
    private PowerManager.WakeLock wakelock;

    public AmarenaAndroidEngine() {

    }

    public void loadEngine() {
        this.loadEngine(false, null, new FillResolutionStrategy());
    }

    public void loadEngine(Config config) {
        this.loadEngine(false, config, new FillResolutionStrategy());
    }

    public void loadEngine(Config config, ResolutionStrategy resolutionStrategy) {
        this.loadEngine(false, config, resolutionStrategy);
    }

    public void loadEngine(boolean useGL2IfAvailable, Config config, ResolutionStrategy resolutionStrategy) {
        if (config == null)
            config = new Config();

        this.amarenaEngine = new AmarenaEngine() {
            @Override
            public void onSurfaceCreated() {
                AmarenaAndroidEngine.this.surfaceCreated();
            }

            @Override
            public void onSurfaceChanged(int width, int height) {
                AmarenaAndroidEngine.this.surfaceChanged(width, height);
            }
        };
        super.initialize(amarenaEngine, useGL2IfAvailable, resolutionStrategy, 20);
        this.amarenaEngine.init(this, config);


    }

    public abstract void surfaceCreated();

    public void surfaceChanged(int width, int height) {

    }

    public AmarenaEngine engine() {
        return amarenaEngine;
    }

    public void setFixLandscapeOrientation() {

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    public void setFixPortraitOrientation() {

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    private void acquireWakeLock() {
        final PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        this.wakelock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "AmarenaEngine");
        this.wakelock.acquire();
    }

    private void releaseWakeLock() {
        if (this.wakelock != null) {
            this.wakelock.release();
        }
    }
}
